package deleteface.bank.jqsoft.cn.deleteface;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;


import com.alibaba.fastjson.JSON;
import com.example.zhouwei.library.CustomPopWindow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jqsoft.bank.deleteface.R;
import deleteface.bank.jqsoft.cn.deleteface.ui.FloatBackground;
import deleteface.bank.jqsoft.cn.deleteface.ui.FloatBitmap;
import deleteface.bank.jqsoft.cn.deleteface.ui.FloatCircle;
import deleteface.bank.jqsoft.cn.deleteface.ui.FloatRect;
import deleteface.bank.jqsoft.cn.deleteface.ui.FloatRing;
import deleteface.bank.jqsoft.cn.deleteface.ui.FloatText;
import deleteface.bank.jqsoft.cn.deleteface.ui.NbButton;

import static deleteface.bank.jqsoft.cn.deleteface.MainActivity.JSONToObject;

public class DeleteFaceForMany extends Fragment {
    private RecyclerView recyclerView;
    private NbButton btn_delete_time,btn_delete_archive;
    private DeleteForManyAdapter deleteForManyAdapter;
    private ArrayList<IpBean> ipBeanArrayList=new ArrayList<>();
    private List<String> ipStrings=new ArrayList<>();
    private LinearLayout add_ll;
    ArrayList<IpBean> urlList=new ArrayList<>();
    String message;
    CustomPopWindow mListPopWindow;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_for_many, container, false);
        inData();
        recyclerView =  view.findViewById(R.id.recyclerview);
        add_ll=view.findViewById(R.id.add_ll);
        btn_delete_archive=view.findViewById(R.id.btn_delete_archive);
        ipStrings.add("");
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext() );
        deleteForManyAdapter=new DeleteForManyAdapter(getContext(),ipStrings);
//设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
//设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper. VERTICAL);
//设置Adapter
        recyclerView.setAdapter(deleteForManyAdapter);
        //设置分隔线
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        //设置增加或删除条目的动画
        recyclerView.setItemAnimator( new DefaultItemAnimator());


        final FloatBackground floatBackground = view.findViewById(R.id.float_view);
         btn_delete_time=view.findViewById(R.id.btn_delete_time);
        floatBackground.addFloatView(new FloatRect(0.1f, 0.1f, 170, 30));
        floatBackground.addFloatView(new FloatBitmap( getActivity(), 0.2f, 0.3f, R.drawable.test));
        floatBackground.addFloatView(new FloatBitmap(  getActivity(), 0.8f, 0.3f, R.drawable.test));
        floatBackground.addFloatView(new FloatCircle( 0.8f, 0.8f));
//        floatBackground.addFloatView(new FloatBitmap( this, 0.1f, 0.6f, R.drawable.gr_ptn_03));
        floatBackground.addFloatView(new FloatText( 0.3f, 0.6f, "好好学习！"));
        floatBackground.addFloatView(new FloatText( 0.5f, 0.9f, "减肥十斤！"));
        floatBackground.addFloatView(new FloatText( 0.9f, 0.2f, "努力工作！"));
        floatBackground.addFloatView(new FloatText( 0.1f, 0.2f, "努力工作！"));
        floatBackground.addFloatView(new FloatText( 0.2f, 0.3f, "努力工作！"));
        floatBackground.addFloatView(new FloatText( 0.4f, 0.5f, "努力工作！"));
        floatBackground.addFloatView(new FloatText( 0.6f, 0.7f, "努力工作！"));
        floatBackground.addFloatView(new FloatText( 0.8f, 0.9f, "努力工作！"));
        floatBackground.addFloatView(new FloatRing( 0.4f, 0.8f, 10 ,40));
        floatBackground.addFloatView(new FloatRing( 0.6f, 0.2f, 15 ,20));
//        floatBackground.addFloatView(new FloatBitmap( this, 0.3f, 0.7f, R.drawable.ico_camera));
//        floatBackground.addFloatView(new FloatBitmap( this, 0.8f, 0.2f, R.drawable.gr_ptn_05));
        setListeners();
        btn_delete_archive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btn_delete_archive.startAnim();
            }
        });
        btn_delete_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btn_delete_time.startAnim();
                new Thread(netReadCard).start();
//                floatBackground.startFloat();
            }
        });
        add_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ipStrings.add("");
                deleteForManyAdapter.notifyDataSetChanged();
            }
        });
        return view;
    }

    private void inData() {

        urlList.add(new IpBean("固镇","http://218.22.110.114:8801/delete",
                "http://218.22.110.114:9093/fdss-api/face/delReadCardInfoByCardNo","218.22.110.114"));
        urlList.add(new IpBean("肥东","http://218.22.56.86:8801/delete",
                "http://218.22.56.86:9093/fdss-api/face/delReadCardInfoByCardNo","218.22.56.86"));
        urlList.add(new IpBean("泗县","http://60.171.220.164:8801/delete"
                ,"http://60.171.220.164:9093/fdss-api/face/delReadCardInfoByCardNo","60.171.220.164"));
        urlList.add(new IpBean("铜官","http://60.173.1.131:8801/delete",
                "http://60.173.1.131:9093/fdss-api/face/delReadCardInfoByCardNo","60.173.1.131"));
        urlList.add(new IpBean("濉溪","http://117.57.168.206:8801/delete",
                "http://117.57.168.206:9093/fdss-api/face/delReadCardInfoByCardNo","117.57.168.206"));
        urlList.add(new IpBean("太湖","http://183.167.230.159:8801/delete",
                "http://183.167.230.159:9093/fdss-api/face/delReadCardInfoByCardNo","183.167.230.159"));
        urlList.add(new IpBean("岳西","http://60.171.137.137:8801/delete",
                "http://60.171.137.137:9093/fdss-api/face/delReadCardInfoByCardNo","60.171.137.137"));
        urlList.add(new IpBean("舒城","http://60.173.152.105:8801/delete",
                "http://60.173.152.105:9093/fdss-api/face/delReadCardInfoByCardNo","60.173.152.105"));
        urlList.add(new IpBean("颍上","http://220.179.65.153:8801/delete",
                "http://220.179.65.153:9093/fdss-api/face/delReadCardInfoByCardNo","220.179.65.153"));
        urlList.add(new IpBean("颍东","http://220.179.84.235:8801/delete",
                "http://220.179.84.235:9093/fdss-api/face/delReadCardInfoByCardNo","220.179.84.235"));
        urlList.add(new IpBean("太和","http://220.179.90.8:8801/delete",
                "http://220.179.90.8:9093/fdss-api/face/delReadCardInfoByCardNo","220.179.90.8"));
        urlList.add(new IpBean("颖泉","http://220.179.82.42:8801/delete",
                "http://220.179.82.42:9093/fdss-api/face/delReadCardInfoByCardNo","220.179.82.42"));
        urlList.add(new IpBean("广德","http://60.173.82.157:8801/delete",
                "http://60.173.82.157:9093/fdss-api/face/delReadCardInfoByCardNo","60.173.82.157"));
        urlList.add(new IpBean("怀远","http://218.22.94.164:8801/delete",
                "http://218.22.94.164:9093/fdss-api/face/delReadCardInfoByCardNo","218.22.94.164"));
        urlList.add(new IpBean("颍州","http://220.180.153.108:8801/delete",
                "http://220.180.153.108:9093/fdss-api/face/delReadCardInfoByCardNo","220.180.153.108"));
        urlList.add(new IpBean("潜山","http://183.161.35.44:8801/delete",
                "http://183.161.35.44:9093/fdss-api/face/delReadCardInfoByCardNo","183.161.35.44"));
    }


    /**
     * 设置监听
     */
    private void setListeners() {
        if (deleteForManyAdapter != null){
            // 注册监听器,回调用来刷新数据显示
            deleteForManyAdapter.setDelItemListener(new DeleteForManyAdapter.DeleteItem() {
                @Override
                public void delete(int pos) {
                    ipStrings.remove(pos);
                    deleteForManyAdapter.notifyDataSetChanged();
                }
            });
        }
    }

    private Runnable netReadCard = new Runnable() {
        @Override
        public void run() {
//            deleteTime();
        }
    };
//    /**
//     * 删除有效期
//     */
//    private void deleteTime(){
//
//        String idCardNumber =etIdCardToDelete.getText().toString();
//        if ( TextUtils.isEmpty(idCardNumber)||TextUtils.isEmpty(timeurl)){
//            myhandler.sendEmptyMessage(1);
//
//        }else {
//
//            String strBase64 = deleteface.bank.jqsoft.cn.deleteface.Base64.encode(idCardNumber);
//
//            Map<String, String> params = new HashMap<>();
//            params.put("cardNo", strBase64);
////            Gson gson = new Gson();
////            String jsonStr = gson.toJson(params);
//            String jsonStr = JSON.toJSONString(params);
//            String s = OKHttpUtil.postJson(timeurl,
//                    jsonStr);
//            HttpBean httpBean=(HttpBean)JSONToObject(s,HttpBean.class);
//
//
//            try {
//                message=  deleteface.bank.jqsoft.cn.deleteface.Base64.decode( httpBean.getMessage());
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            myhandler.sendEmptyMessage(0);
//
//        }
//
//    }


    private Handler myhandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(),message,Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(getActivity(),"身份证号和地址不能为空!",Toast.LENGTH_SHORT).show();
                    break;

            }

        }
    };

}
